package com.hxd.otc.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.TextView;

import com.hxd.otc.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 包  名 :  com.hxd.otc.widget
 * 类  名 :  MultipleBlocksDialog
 * 作  者 :  胡庆岭
 * 时  间 :  2018/6/7 11:54
 * 描  述 :  ${双按钮点击Dialog}
 */
public class MultipleBlocksDialog extends Dialog {
    @BindView(R.id.tv_title)
    TextView mTitleText;
    @BindView(R.id.content_tv)
    TextView mContentText;
    @BindView(R.id.agree_cb)
    CheckBox mAgreeCheck;
    @BindView(R.id.tv_cancel)
    TextView mCancelText;
    @BindView(R.id.tv_sure)
    TextView mSureText;
    @BindView(R.id.view)
    View view;
    @BindView(R.id.view_hor_line)
    View viewHorLine;
    @BindView(R.id.view_content)
    View viewContent;
    private Unbinder mBind;

    public void setOnDialogItemClickListener(OnDialogItemClickListener onDialogItemClickListener) {
        this.onDialogItemClickListener = onDialogItemClickListener;
    }

    private OnDialogItemClickListener onDialogItemClickListener;

    public MultipleBlocksDialog(@NonNull Context context) {
        this(context, R.style.BlockDialog);
    }

    public MultipleBlocksDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.weight_multiple_blocks_dialog);
        mBind = ButterKnife.bind(this);
        initWindows();
    }

    private void initWindows() {
        setCanceledOnTouchOutside(true);
        Window window = getWindow();
        if (window != null) {
            DisplayMetrics dm = new DisplayMetrics();
            window.getWindowManager().getDefaultDisplay().getMetrics(dm);
            WindowManager.LayoutParams windowAttributes = window.getAttributes();
            windowAttributes.width = WindowManager.LayoutParams.MATCH_PARENT;
            windowAttributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
            windowAttributes.gravity = Gravity.CENTER;
            window.setAttributes(windowAttributes);
        }
    }

    @Override
    public void show() {
        super.show();
    }


    public MultipleBlocksDialog setCbText(@NonNull String title) {
        mAgreeCheck.setVisibility(View.VISIBLE);
        mAgreeCheck.setText(title);
        return this;
    }


    public MultipleBlocksDialog setTitle(@NonNull String title) {
        mTitleText.setText(title);
        return this;
    }

    public MultipleBlocksDialog setTitleGone() {
        mTitleText.setVisibility(View.GONE);
        viewContent.setVisibility(View.VISIBLE);
        return this;
    }

    public MultipleBlocksDialog setTitlewithOutPading(@NonNull String title) {
        mTitleText.setText(title);
        view.setVisibility(View.GONE);
        viewHorLine.setVisibility(View.VISIBLE);
        return this;
    }


    public MultipleBlocksDialog setContent(@NonNull String content) {
        mContentText.setText(content);
        return this;
    }

    public MultipleBlocksDialog setContent(@NonNull CharSequence content) {
        mContentText.setText(content);
        return this;
    }

    public MultipleBlocksDialog setTvCancle(String str) {
        mCancelText.setText(str);
        return this;
    }

    public MultipleBlocksDialog setTvCancle(String str, View.OnClickListener listener) {
        mCancelText.setText(str);
        setTvCancle(str, listener);
        return this;
    }

    public MultipleBlocksDialog setTvSure(View.OnClickListener listener) {
        mSureText.setOnClickListener(listener);
        return this;
    }

    public MultipleBlocksDialog setTvSure(String str, View.OnClickListener listener) {
        mSureText.setText(str);
        mSureText.setOnClickListener(listener);
        return this;
    }

    public CheckBox getmAgreeCheck() {
        return mAgreeCheck;
    }

    @OnClick({R.id.tv_cancel, R.id.tv_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                if (onDialogItemClickListener != null) {
                    onDialogItemClickListener.onCancel();
                }
                dismiss();
                break;
            case R.id.tv_sure:
                if (onDialogItemClickListener != null)
                    onDialogItemClickListener.onSure();
                dismiss();
                break;
        }
    }


    public void onDestroy() {
        if (mBind != null) {
            mBind.unbind();
        }
    }

    public interface OnDialogItemClickListener {
        void onSure();

        void onCancel();
    }
}
